package com.geccocrawler.gecco.demo.anjuke;

import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.scheduler.DeriveSchedulerContext;
import com.geccocrawler.gecco.utils.ConnectDb;
import org.apache.commons.dbutils.QueryRunner;
import org.jsoup.helper.StringUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by 抽离 on 2017/5/20 0020.
 */

@PipelineName("houseNextPipeline")
public class HouseNextPipeline implements Pipeline<AnjukeHouse> {

    Connection connection = ConnectDb.Connect();

    @Override
    public void process(AnjukeHouse bean) {
        HttpRequest request = bean.getHttpRequest();
        QueryRunner queryRunner = new QueryRunner();
        String sql = "insert into house values(null, ?, ?, ?)";
        List<String> imgUrls = bean.getImgUrl();
        List<String> titles = bean.getTitle();
        List<String> url = bean.getUrl();
        for(int i = 0; i <titles.size(); i ++){
            try {
                queryRunner.update(connection, sql, imgUrls.get(i), titles.get(i), url.get(i));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        String nextUrl = bean.getNextUrl();
        if(!StringUtil.isBlank(nextUrl)) {
            DeriveSchedulerContext.into(request.subRequest(nextUrl));
        }
    }
}
